package system

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type SysUserDataAuth struct {
	SysUserDataAuthId int       `orm:"column(sys_user_data_auth_id);pk"`
	SysUserId         int       `description:"系统用户id"`
	CampusId          int       `description:"校区id"`
	ClassId           int       `description:"班级id"`
	CreateTime        time.Time `description:"创建时间"`
	ModifyTime        time.Time `description:"修改时间"`
}

func (obj *SysUserDataAuth) SaveSysUserDataAuth(req *DataAuthSave) (err error) {
	o := orm.NewOrm()
	list := make([]*SysUserDataAuth, 0)
	for _, v := range req.CampusList {
		for _, cv := range v.ClassList {
			item := new(SysUserDataAuth)
			item.SysUserId = int(req.SysUserId)
			item.CampusId = v.CampusId
			item.ClassId = cv.ClassId
			item.CreateTime = time.Now()
			item.ModifyTime = time.Now()
			list = append(list, item)
		}
	}
	_, err = o.InsertMulti(len(list), &list)
	if err != nil {
		return err
	}
	return
}

func (obj *SysUserDataAuth) GetSysUserDataAuth(sysUserId int) (items []*SysUserDataAuth, err error) {
	o := orm.NewOrm()
	sql := `SELECT *
			FROM sys_user_data_auth
			WHERE sys_user_id=? `
	_, err = o.Raw(sql, sysUserId).QueryRows(&items)
	return
}
